<template>
  <div class="app-container">
    <el-form ref="noticedemandapprovalRef" :model="formData" :rules="rules" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="立案号" prop="caseId">
            <el-select
              v-model="formData.caseId"
              placeholder="请选择立案号"
              :disabled="disabled"
              clearable
              @change="handleCase">
              <el-option v-for="item in numberList" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="案由" prop="caseCause">
            <el-input v-model="formData.hearingRecord.caseCause" disabled placeholder="请输入案由" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="听证告知书文号" prop="hearingNoticeNo">
            <el-input v-model="formData.hearingRecord.hearingNoticeNo" disabled placeholder="请输入听证告知书文号" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="听证通知书文号" prop="hearingNoticeDocNo">
            <el-input v-model="formData.hearingRecord.hearingNoticeDocNo" disabled placeholder="请输入听证通知书文号" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="听证时间" prop="daterangeApplyTime">
            <el-date-picker
              clearable
              v-model="formData.daterangeApplyTime"
              type="datetimerange"
              value-format="YYYY-MM-DD HH:mm:ss"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="听证地点" prop="hearingAddress">
            <el-input v-model="formData.hearingRecord.hearingAddress" disabled placeholder="请输入听证地点" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="主持人" prop="hostName">
            <el-input v-model="formData.hearingRecord.hostName" disabled placeholder="请输入主持人姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="主持人工作单位" prop="hostUnit">
            <el-input v-model="formData.hearingRecord.hostUnit" disabled placeholder="请输入主持人工作单位" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="主持人职务" prop="hostPosition">
            <el-input v-model="formData.hearingRecord.hostPosition" disabled placeholder="请输入主持人职务" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="记录人" prop="recorderName">
            <el-input v-model="formData.hearingRecord.recorderName" disabled placeholder="请输入记录人姓名" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="记录人工作单位" prop="recorderUnit">
            <el-input v-model="formData.hearingRecord.recorderUnit" disabled placeholder="请输入记录人工作单位" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="记录人职务" prop="recorderPosition">
            <el-input v-model="formData.hearingRecord.recorderPosition" disabled placeholder="请输入记录人职务" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="当事人名称" prop="partyName">
            <el-input v-model="formData.hearingRecord.partyName" disabled placeholder="请输入当事人名称" />
          </el-form-item>
        </el-col>
        <el-col :span="16">
          <el-form-item label="地址" :rules="rules.addressObj" prop="addressObj">
            <locationSelect v-model="formData.addressObj" disabled style="width: 100%" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="法定代表人" prop="legalRepresentative">
            <el-input v-model="formData.hearingRecord.legalRepresentative" disabled placeholder="请输入法定代表人" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="职务" prop="position">
            <el-input v-model="formData.hearingRecord.position" disabled placeholder="请输入职务" />
          </el-form-item>
        </el-col>
      </el-row>
      <template v-for="(item, index) in formData.hearingRecord.agentPersions" :key="item.id">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="委托代表人姓名" :prop="`agentPersions[${index}].agentName`" :rules="rules.agentName">
              <el-input v-model="item.agentName" disabled placeholder="请输入委托代表人姓名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电话" :prop="`agentPersions[${index}].agentPhone`" :rules="rules.agentPhone">
              <el-input v-model="item.agentPhone" disabled placeholder="请输入委托代表人电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="委托代表人工作单位"
              :prop="`agentPersions[${index}].agentUnit`"
              :rules="rules.agentUnit"
              class="long-label">
              <el-input v-model="item.agentUnit" disabled placeholder="请输入委托代表人工作单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="委托代表人职务"
              :prop="`agentPersions[${index}].agentPosition`"
              :rules="rules.agentPosition">
              <el-input v-model="item.agentPosition" disabled placeholder="请输入委托代表人职务" />
            </el-form-item>
          </el-col>
          <el-col :span="8" style="padding-top: 6px">
            <el-button
              v-if="index === formData.hearingRecord.agentPersions.length - 1"
              type="primary"
              link
              disabled
              icon="CirclePlus"
              @click="handleAddPresent" />
            <el-button
              v-if="formData.hearingRecord.agentPersions.length > 1"
              type="danger"
              link
              disabled
              icon="delete"
              @click="handleRemovePresent(item)" />
          </el-col>
        </el-row>
      </template>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="案件调查人" prop="investigator1Name">
            <el-select
              v-model="formData.hearingRecord.investigator1Name"
              filterable
              clearable
              disabled
              placeholder="请输入执法人员"
              style="width: calc(100% - 45px); margin-right: 6px"
              @change="handlepartyChange1">
              <el-option v-for="item in inspectorOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="执法证编号" prop="investigator1No">
            <el-input v-model="formData.hearingRecord.investigator1No" disabled placeholder="请输入执法证编号" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工作单位" prop="investigator1Unit">
            <el-input v-model="formData.hearingRecord.investigator1Unit" disabled placeholder="请输入工作单位" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="案件调查人" prop="investigator2Name">
            <el-select
              v-model="formData.hearingRecord.investigator2Name"
              filterable
              clearable
              disabled
              placeholder="请输入执法人员"
              style="width: calc(100% - 45px); margin-right: 6px"
              @change="handlepartyChange2">
              <el-option v-for="item in inspectorOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="执法证编号" prop="investigator2No">
            <el-input v-model="formData.hearingRecord.investigator2No" disabled placeholder="请输入执法证编号" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工作单位" prop="investigator2Unit">
            <el-input v-model="formData.hearingRecord.investigator2Unit" disabled placeholder="请输入工作单位" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div class="form-title-text-two" style="margin-bottom: 20px">当事人申辩质证的主要内容</div>
        </el-col>
        <CaseSituation
          ref="caseSituationRef"
          v-model="formData.hearingRecord"
          :disabled="true"
          :show="'案情说明, 证据证明, 违反条例, 处理依据, 陈述申辩'"
          :caseIsEdit="true"
          :stateIsEdit="true"
          :isEdit="true"
          :violationIsEdit="true" />
        <el-col :span="24">
          <el-form-item label="争论焦点问题" prop="disputeFocusIssues">
            <el-input
              v-model="formData.disputeFocusIssues"
              :disabled="disabled"
              type="textarea"
              :rows="3"
              placeholder="请输入争论焦点问题" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="主持人意见和建议" prop="hostSuggestion">
            <el-input
              v-model="formData.hostSuggestion"
              :disabled="disabled"
              type="textarea"
              :rows="3"
              placeholder="请输入主持人意见和建议" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, watch } from 'vue'
import {
  getHearingReport,
  addHearingReport,
  updateHearingReport,
  getCaseList,
  getContent,
  previewPdf,
} from '@/api/hearingNotice/report.js'
import { registrationcaseDetail } from '@/api/filingReview/register'
import { getUserEnforcementNo } from '@/api/system/user.js'
import { ElMessage } from 'element-plus'
import locationSelect from '@/components/locationSelect/index.vue'
import CaseSituation from '@/components/CaseSituation/index.vue'
import { commonRules } from '@/components/DynamicForm/config'

const { proxy } = getCurrentInstance()

// Props
const props = defineProps({
  id: {
    type: [String, Number],
    default: undefined,
  },
  disabled: {
    type: Boolean,
    default: false,
  },
})

// Emits
const emit = defineEmits(['handleSubmit'])

// Form data
const formData = ref({
  id: null,
  caseId: null,
  caseNo: null,

  daterangeApplyTime: [],
  partyName: null,
  streetOfCompanyAddress: null,
  address: null,

  statementDefense: null,
  investigator1Name: null,
  investigator1No: null,
  investigator1Unit: null,
  investigator2Name: null,
  investigator2No: null,
  investigator2Unit: null,
  disputeFocusIssues: null,
  hostSuggestion: null,
  hearingRecord: {
    position: null,
    legalRepresentative: null,
    caseCause: null,
    caseDesc: null,
    hearingNoticeNo: null,
    hearingNoticeDocNo: null,
    evidenceDesc: null,
    violationRegulations: null,
    handlingBasis: null,
    hearingNoticeDocId: null,
    hostName: null,
    hostUnit: null,
    hostPosition: null,
    recorderName: null,
    recorderUnit: null,
    recorderPosition: null,
    hearingAddress: null,
    partyPleadProcess: null,
    agentPersions: [
      {
        id: Date.now(),
        agentName: '',
        agentPhone: '',
        agentUnit: '',
        agentPosition: '',
      },
    ],
  },
})
// 自定义校验器：检查拍摄地点是否完整
const validateLocation = (rule, value, callback) => {
  if (!value || !value.streetOfPhotoLocation || !value.photoLocation) {
    callback(new Error('请选择地点'))
  } else {
    callback()
  }
}
const validateTime = (rule, value, callback) => {
  if (!formData.value.daterangeApplyTime || formData.value.daterangeApplyTime.length !== 2) {
    callback(new Error('请选择时间'))
  } else {
    // 验证是否是同一天
    const startDate = new Date(formData.value.daterangeApplyTime[0])
    const endDate = new Date(formData.value.daterangeApplyTime[1])

    const isSameDay =
      startDate.getFullYear() === endDate.getFullYear() &&
      startDate.getMonth() === endDate.getMonth() &&
      startDate.getDate() === endDate.getDate()

    if (!isSameDay) {
      callback(new Error('时间不能跨天'))
    } else if (startDate.getTime() >= endDate.getTime()) {
      callback(new Error('结束时间必须大于开始时间'))
    } else {
      callback()
    }
  }
}
// Form validation rules
const rules = {
  // caseNo: [{ required: true, message: '立案号不能为空', trigger: ['blur', 'change'] }],
  // caseCause: [{ required: true, message: '案由不能为空', trigger: ['blur', 'change'] }],
  // hearingNoticeNo: [{ required: true, message: '听证告知书文号不能为空', trigger: ['blur', 'change'] }],
  // hearingNoticeDocNo: [{ required: true, message: '听证通知书文号不能为空', trigger: ['blur', 'change'] }],
  daterangeApplyTime: [{ required: true, validator: validateTime, trigger: ['blur', 'change'] }],
  // hearingAddress: [{ required: true, message: '听证地点不能为空', trigger: ['blur', 'change'] }],
  // hostName: [{ required: true, message: '主持人姓名不能为空', trigger: ['blur', 'change'] }],
  // hostUnit: [{ required: true, message: '主持人工作单位不能为空', trigger: ['blur', 'change'] }],
  // hostPosition: [{ required: true, message: '主持人职务不能为空', trigger: ['blur', 'change'] }],
  // recorderName: [{ required: true, message: '记录人姓名不能为空', trigger: ['blur', 'change'] }],
  // recorderUnit: [{ required: true, message: '记录人工作单位不能为空', trigger: ['blur', 'change'] }],
  // recorderPosition: [{ required: true, message: '记录人职务不能为空', trigger: ['blur', 'change'] }],
  // partyName: [{ required: true, message: '当事人名称不能为空', trigger: ['blur', 'change'] }],
  // caseDesc: [{ required: true, message: '请输入案情说明', trigger: ['blur', 'change'] }],
  // evidenceDesc: [{ required: true, message: '请输入证据证明', trigger: ['blur', 'change'] }],
  // violationRegulations: [{ required: true, message: '请输入违反条例', trigger: ['blur', 'change'] }],
  // statementDefense: [{ required: true, message: '请输入陈述申辩告知', trigger: ['blur', 'change'] }],
  // handlingBasis: [{ required: true, message: '请输入处理依据', trigger: ['blur', 'change'] }],
  // legalRepresentative: [{ required: true, message: '法定代表人不能为空', trigger: ['blur', 'change'] }],
  // position: [{ required: true, message: '职务不能为空', trigger: ['blur', 'change'] }],
  // addressObj: [{ required: true, validator: validateLocation, trigger: ['blur', 'change'] }],
  // agentName: [{ required: true, message: '委托代表人姓名不能为空', trigger: ['blur', 'change'] }],
  // agentPhone: [commonRules.phone],
  // agentUnit: [{ required: true, message: '委托代表人工作单位不能为空', trigger: ['blur', 'change'] }],
  // agentPosition: [{ required: true, message: '委托代表人职务不能为空', trigger: ['blur', 'change'] }],
  // investigator1Name: [{ required: true, message: '案件调查人姓名不能为空', trigger: ['blur', 'change'] }],
  // investigator1No: [{ required: true, message: '案件调查人执法证编号不能为空', trigger: ['blur', 'change'] }],
  // investigator1Unit: [{ required: true, message: '案件调查人工作单位不能为空', trigger: ['blur', 'change'] }],
  // investigator2Name: [{ required: true, message: '案件调查人姓名不能为空', trigger: ['blur', 'change'] }],
  // investigator2No: [{ required: true, message: '案件调查人执法证编号不能为空', trigger: ['blur', 'change'] }],
  // investigator2Unit: [{ required: true, message: '案件调查人工作单位不能为空', trigger: ['blur', 'change'] }],
  disputeFocusIssues: [{ required: true, message: '争论焦点问题不能为空', trigger: ['blur', 'change'] }],
  hostSuggestion: [{ required: true, message: '主持人意见和建议不能为空', trigger: ['blur', 'change'] }],
}

// Refs
const noticedemandapprovalRef = ref()

// Methods
const reset = () => {
  formData.value = {
    id: null,
    caseId: null,
    caseNo: null,

    daterangeApplyTime: [],
    streetOfCompanyAddress: null,
    address: null,

    statementDefense: null,
    disputeFocusIssues: null,
    hostSuggestion: null,
    hearingRecord: {
      position: null,
      legalRepresentative: null,
      caseCause: null,
      partyName: null,
      caseDesc: null,
      hearingNoticeNo: null,
      hearingNoticeDocNo: null,
      evidenceDesc: null,
      violationRegulations: null,
      handlingBasis: null,
      hearingNoticeDocId: null,
      hostName: null,
      hostUnit: null,
      hostPosition: null,
      recorderName: null,
      recorderUnit: null,
      recorderPosition: null,
      hearingAddress: null,
      partyPleadProcess: null,
      agentPersions: [
        {
          id: Date.now(),
          agentName: '',
          agentPhone: '',
          agentUnit: '',
          agentPosition: '',
        },
      ],
    },
  }
  proxy.resetForm('noticedemandapprovalRef')
}

// 委托代表人 list 操作
const handleAddPresent = () => {
  formData.value.hearingRecord.agentPersions.push({
    id: Date.now(),
    agentName: '',
    agentPhone: '',
    agentUnit: '',
    agentPosition: '',
  })
}
const handleRemovePresent = item => {
  formData.value.hearingRecord.agentPersions = formData.value.hearingRecord.agentPersions.filter(i => i.id !== item.id)
}

// 案件调查人
const inspectorOptions = ref([])
const getUserList = async () => {
  const res = await getUserEnforcementNo({})
  if (res.data.length > 0) {
    inspectorOptions.value = res.data.map(item => {
      return { value: item.name, label: item.name, enforcementNo: item.enforcementNo }
    })
  }
}
getUserList()

const handlepartyChange1 = async val => {
  if (val === formData.value.investigator2Name) {
    formData.value.investigator1Name = null
    formData.value.investigator1No = null
    return
  }
  inspectorOptions.value.forEach(item => {
    if (item.value === val) {
      formData.value.investigator1No = item.enforcementNo
    }
  })
}
const handlepartyChange2 = async val => {
  if (val === formData.value.investigator1Name) {
    formData.value.investigator2Name = null
    formData.value.investigator2No = null
    return
  }
  inspectorOptions.value.forEach(item => {
    if (item.value === val) {
      formData.value.investigator2No = item.enforcementNo
    }
  })
}

// Get detail data
const getDetail = async id => {
  reset()
  if (id) {
    try {
      const res = await getHearingReport(id)
      if (res.data.hearingStartTime && res.data.hearingEndTime) {
        res.data.daterangeApplyTime = [res.data.hearingStartTime, res.data.hearingEndTime]
      } else {
        res.data.daterangeApplyTime = []
      }
      formData.value = res.data
      formData.value.addressObj = {
        photoLocation: res.data.hearingRecord.address,
        streetOfPhotoLocation: res.data.hearingRecord.streetOfCompanyAddress,
      }
      formData.value.hearingRecord.statementDefense = res.data.hearingRecord.partyPleadProcess
    } catch (error) {
      console.error('获取详情失败:', error)
    }
  }
}

const handleCase = async val => {
  return new Promise(async (resolve, reject) => {
    if (!val) {
      formData.value.caseNo = ''
      formData.value.hearingRecord.caseCause = ''
      formData.value.hearingRecord.hearingNoticeNo = ''
      formData.value.hearingRecord.hearingNoticeDocNo = ''
      formData.value.hearingRecord.partyName = ''
      formData.value.partyId = ''
      formData.value.hearingRecord.legalRepresentative = ''
      formData.value.hearingRecord.position = ''
      formData.value.hearingRecord.hostName = ''
      formData.value.hearingRecord.hostUnit = ''
      formData.value.hearingRecord.hostPosition = ''
      formData.value.hearingRecord.recorderName = ''
      formData.value.hearingRecord.recorderUnit = ''
      formData.value.hearingRecord.recorderPosition = ''
      formData.value.addressObj = {
        photoLocation: '',
        streetOfPhotoLocation: '',
      }
      formData.value.hearingRecord.hearingAddress = ''
      formData.value.hearingRecord.agentPersions = []
      return
    }
    const res = await registrationcaseDetail(val)
    const { partyId, streetOfCompanyAddress, address } = res.data
    formData.value.caseNo = res.data.caseNo
    formData.value.partyId = partyId
    formData.value.addressObj = {
      photoLocation: address,
      streetOfPhotoLocation: streetOfCompanyAddress,
    }
    const res2 = await getContent(val)
    formData.value.hearingRecord.position = res2.data.hearingRecord.position
    formData.value.hearingRecord.legalRepresentative = res2.data.hearingRecord.legalRepresentative
    formData.value.hearingRecord.caseCause = res2.data.hearingRecord.caseCause
    formData.value.hearingRecord.partyName = res2.data.hearingRecord.partyName
    formData.value.hearingRecord.caseDesc = res2.data.caseDesc || res2.data.hearingRecord.caseDesc
    formData.value.hearingRecord.hearingNoticeNo = res2.data.hearingNoticeNo || res2.data.hearingRecord.hearingNoticeNo
    formData.value.hearingRecord.hearingNoticeDocNo =
      res2.data.hearingNoticeDocNo || res2.data.hearingRecord.hearingNoticeDocNo
    formData.value.hearingRecord.evidenceDesc = res2.data.evidenceDesc || res2.data.hearingRecord.evidenceDesc
    formData.value.hearingRecord.violationRegulations =
      res2.data.violationRegulations || res2.data.hearingRecord.violationRegulations
    formData.value.hearingRecord.handlingBasis = res2.data.handlingBasis || res2.data.hearingRecord.handlingBasis
    formData.value.hearingRecord.hearingNoticeDocId =
      res2.data.hearingNoticeDocId || res2.data.hearingRecord.hearingNoticeDocId
    formData.value.hearingRecord.hostName = res2.data.hostName || res2.data.hearingRecord.hostName
    formData.value.hearingRecord.hostUnit = res2.data.hostUnit || res2.data.hearingRecord.hostUnit
    formData.value.hearingRecord.hostPosition = res2.data.hostPosition || res2.data.hearingRecord.hostPosition
    formData.value.hearingRecord.recorderName = res2.data.recorderName || res2.data.hearingRecord.recorderName
    formData.value.hearingRecord.recorderUnit = res2.data.recorderUnit || res2.data.hearingRecord.recorderUnit
    formData.value.hearingRecord.recorderPosition =
      res2.data.recorderPosition || res2.data.hearingRecord.recorderPosition
    formData.value.hearingRecord.hearingAddress = res2.data.hearingAddress || res2.data.hearingRecord.hearingAddress
    formData.value.hearingRecord.statementDefense = res2.data.hearingRecord.partyPleadProcess
    formData.value.hearingRecordId = res2.data.hearingRecordId
    formData.value.hearingRecord.agentPersions = res2.data.hearingRecord.agentPersions
    formData.value.hearingRecord.investigator1Name = res2.data.hearingRecord.investigator1Name || ''
    formData.value.hearingRecord.investigator1No = res2.data.hearingRecord.investigator1No || ''
    formData.value.hearingRecord.investigator1Unit = res2.data.hearingRecord.investigator1Unit || ''
    formData.value.hearingRecord.investigator2Name = res2.data.hearingRecord.investigator2Name || ''
    formData.value.hearingRecord.investigator2No = res2.data.hearingRecord.investigator2No || ''
    formData.value.hearingRecord.investigator2Unit = res2.data.hearingRecord.investigator2Unit || ''
    nextTick(() => {
      caseSituationRef.value.pullCaseDescOnly()
    })
    resolve()
  })
}

const handlePreview = () => {
  return new Promise(resolve => {
    submitForm(true)
      .then(async params => {
        previewPdf(params)
          .then(res => {
            if (!!res.msg) {
              window.open(res.msg)
              resolve()
            } else {
              ElMessage.warning('预览文件不存在')
              resolve()
            }
          })
          .catch(() => {
            ElMessage.warning('预览文件不存在')
            resolve()
          })
      })
      .catch(() => {
        ElMessage.warning('请先完善表单信息')
        resolve()
      })
  })
}

// Submit form
const caseSituationRef = ref(null)
const submitForm = async (isPreview = false) => {
  return new Promise((resolve, reject) => {
    console.log('提交数据', formData.value)
    noticedemandapprovalRef.value
      .validate()
      .then(async () => {
        const params = JSON.parse(JSON.stringify(formData.value))
        if (params.daterangeApplyTime.length !== 2) {
          params.hearingStartTime = null
          params.hearingEndTime = null
        } else {
          params.hearingStartTime = params.daterangeApplyTime[0]
          params.hearingEndTime = params.daterangeApplyTime[1]
        }
        params.hearingRecord.partyPleadProcess = params.hearingRecord.statementDefense
        formData.value.address = formData.value.addressObj.photoLocation
        formData.value.streetOfCompanyAddress = formData.value.addressObj.streetOfPhotoLocation
        if (isPreview === true) {
          resolve(params)
          return
        }
        let result
        if (params.id) {
          result = await updateHearingReport(params)
        } else {
          result = await addHearingReport(params)
        }

        ElMessage.success('提交成功')
        emit('handleSubmit', result)
        return result
      })
      .catch(err => {
        console.log('验证失败', err)
        reject(err)
      })
  })
}

// Watch props.id change
watch(
  () => props.id,
  newVal => {
    if (newVal) {
      getDetail(newVal)
    } else {
      reset()
    }
    handleCaseList()
  },
  { immediate: true }
)

const numberList = ref([])
async function handleCaseList() {
  const res = await getCaseList()
  numberList.value = res.data.map(item => ({
    label: item.caseNo,
    value: item.id,
  }))
}
// Expose methods
defineExpose({
  submitForm,
  handlePreview,
  reset,
  formData,
})
</script>

<style scoped>
.app-container {
  padding: 20px;
}
</style>
